package Beginning;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.ParsePosition;

class AL extends JFrame implements WindowListener,ActionListener {
    JTextField text1 = new JTextField(20);
    JTextField text2 = new JTextField(20);
    JTextField text3 = new JTextField(20);
    Button b;

    public static void main(String[] args) {
            AL myWindow = new AL("My first window");
            myWindow.setSize(350,350);                
            myWindow.setVisible(true);
    }

    public AL(String title) {
            super(title);
            setLayout(new FlowLayout());
            addWindowListener(this);
            b = new Button("Click me");
            add(b);
            b.addActionListener(this);
            add(text1);
            add(text2);
            add(text3);
    }

    public int Sum(int a, int b) {
		int sum = 0;
		sum = a + b;
		return sum;
	}
    
    public void actionPerformed(ActionEvent e) {
    	JOptionPane abc = new JOptionPane();
    	String strText1 = text1.getText();
    	String strText2 = text2.getText();
    	if(!text1.getText().equals("") && !text2.getText().equals(""))
    	{
    		if(isNumeric(text1.getText()) && isNumeric(text2.getText()))
    		{
    			int sum = Integer.parseInt(strText1) + Integer.parseInt(strText2);
        		text3.setText(Integer.toString(sum));
    		}
    		else
    		{
    			abc.showMessageDialog(new Dialog(this), "Chi duoc nhap so nguyen");
    		}
    	}
    	else
    	{
    		abc.showMessageDialog(new Dialog(this), "Vui long nhap day du 2 so");
    	}
    }

    public static boolean isNumeric(String str)
    {
      NumberFormat formatter = NumberFormat.getInstance();
      ParsePosition pos = new ParsePosition(0);
      formatter.parse(str, pos);
      return str.length() == pos.getIndex();
    }
    
    public void windowClosing(WindowEvent e) {
            dispose();
            System.exit(0);
    }
    
    public void windowOpened(WindowEvent e) {}
   
    public void windowActivated(WindowEvent e) {}
    
    public void windowIconified(WindowEvent e) {}
    
    public void windowDeiconified(WindowEvent e) {}
    
    public void windowDeactivated(WindowEvent e) {}
    
    public void windowClosed(WindowEvent e) {}
}
